package com.qf.admin.servlet;

import com.qf.admin.domain.AddAddress;
import com.qf.admin.domain.Address;
import com.qf.admin.domain.Orders;
import com.qf.admin.service.AddressService;
import com.qf.admin.service.impl.AddressServiceImpl;
import com.qf.base.BaseServlet;
import com.qf.commons.TableData;
import com.qf.utils.CookieUtils;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@WebServlet("/address")
public class AddressServlet extends BaseServlet {
    private AddressService addressService = new AddressServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String methodName = req.getParameter("method");

        try {
            Method method = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(this, req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    ////获取订单对应用户的信息
    public void getUserAddress(HttpServletRequest req, HttpServletResponse resp){

        String userId = req.getParameter("userId");
        Orders addressUser = addressService.getAddressUser(Integer.parseInt(userId));
        

        try {
            sendJson(resp,addressUser);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    //改变订单的地址
   public void changeAddress(HttpServletRequest req, HttpServletResponse resp){
       Map<String, Object> map = new HashMap<>();
       String userId = req.getParameter("userId");
       String aid = req.getParameter("aid");
       String changeId = req.getParameter("changeId");
       System.out.println(changeId);

       try {
           addressService.changeOrderAddress(Integer.parseInt(userId),Integer.parseInt(aid),Integer.parseInt(changeId));
           map.put("code", 1);
           map.put("msg", "success");
       }catch (Exception e){
           map.put("code", -1);
           map.put("msg", "fail");
       }

       try {
           sendJson(resp,map);
       } catch (ServletException e) {
           e.printStackTrace();
       } catch (IOException e) {
           e.printStackTrace();
       }

   }


    //获取订单管理的信息
    public void getAddress(HttpServletRequest req, HttpServletResponse resp){

        String name = req.getParameter("name");
        String goods = req.getParameter("goods");
        String number = req.getParameter("number");

        String status = req.getParameter("status");
        String begin = req.getParameter("begin");
        String end = req.getParameter("end");


        // 每页显示的数据,  limit 后的第一个参数 limit 0, 10
        String limit = req.getParameter("limit");
        // 偏移量 limit 后的第一个参数 limit 0, 10
        String offset = req.getParameter("offset");

        if(null == status) {
            status = "-1";
        }
        if(number == null ||"".equals(number.trim())) {
            number = "0";
        }

        Date beginDate = null;
        Date endDate = null;

        if (null != begin && !"".equals(begin.trim())) {
            beginDate = dateFormat(begin, "yyyy-MM-dd HH:mm:ss");
        }

        if (null != end && !"".equals(end.trim())) {
            endDate = dateFormat(end, "yyyy-MM-dd HH:mm:ss");
        }


//        TableData<Orders> address = addressService.getAddress(name,goods,
//                Integer.parseInt(status),Integer.parseInt(number),beginRegisterDate,endRegisterDate,Integer.parseInt(limit),Integer.parseInt(offset));
        TableData<Orders> address = addressService.getAddress(name, goods, Integer.parseInt(status), Integer.parseInt(number), beginDate, endDate, Integer.parseInt(limit), Integer.parseInt(offset));
        System.out.println(address.toString());
        try {
            sendJson(resp,address);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private Date dateFormat(String dateStr, String pattern) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        Date date = null;
        try {
            date = simpleDateFormat.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }


    //获取订单可选地址
    public void getAddAddress(HttpServletRequest req, HttpServletResponse resp){

        String userId1 = req.getParameter("userId");

            List<AddAddress> addAddress = addressService.getAddAddress(Integer.parseInt(userId1));

            System.out.println(addAddress.toString());
        try {
            sendJson(resp, addAddress);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    //获取收货地址
    public void getAddressByUserId(HttpServletRequest req, HttpServletResponse resp){
        String userId=req.getParameter("userId");


        List<Address> addressList=addressService.getAddressByUserId(Integer.parseInt(userId));
        try {
            sendJson(resp,addressList);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
